package com.example.blockchain.service.impl;

import com.example.blockchain.dao.ContractDao;
import com.example.blockchain.entity.TContract;
import com.example.blockchain.entity.TradeObject;
import com.example.blockchain.entity.block.ContractObj;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

@Service
public class ContractServiceImpl {

    @Resource
    private BlockServiceImpl blockService;

    @Resource
    private ContractDao contractDao;

    
    public void setData(TContract contract){
        contractDao.setData(contract);
    }



    public void toChain(TradeObject tradeObject){
        blockService.toChain(tradeObject);
    }


    public static HashMap<String,Object> jsonToMap(String jsonString){
        Gson gson = new Gson();
        Type typeOfMap = new TypeToken<HashMap<String, Object>>(){}.getType();
        HashMap<String, Object> map = gson.fromJson(jsonString, typeOfMap);
        return map;
    }

    public static String mapToString(HashMap<String,Object> map){
        Gson gson = new Gson();
        String jsonString = gson.toJson(map);
        return jsonString;
    }


    public static void main(String[] args) {
        String jsonString = "{\"key1\":\"value1\", \"key2\":123, \"key3\":{\"nestedKey\":\"nestedValue\"}, \"key4\":[\"item1\", \"item2\"]}";

        // 创建 Gson 实例
        Gson gson = new Gson();

        // 将 JSON 字符串转换为 Map<String, Object>
        Type typeOfMap = new TypeToken<HashMap<String, Object>>(){}.getType();
        HashMap<String, Object> map = gson.fromJson(jsonString, typeOfMap);

        // 输出转换后的 Map
        System.out.println(map);
    }

}
